package val.lemonde;

import val.lemonde.FragmentConfiguration.OnConfigSelectedListener;

import com.markupartist.android.widget.ActionBar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;

public class ActivityConfiguration extends ActivityLemondeBase implements
		OnConfigSelectedListener {
	
	private int[] liste = new int[] { R.array.conf_cat_select,
			R.array.conf_cat_order, R.array.conf_view_image,
			R.array.conf_view_desc, R.array.conf_view_date,
			R.array.conf_zoom_enable, R.array.conf_zoom_level };

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("lemonde","ActivityConfiguration Created");
		setContentView(R.layout.configuration);

		ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		getMenuInflater().inflate(R.menu.configuration_top, actionBar.asMenu());
		actionBar.setDisplayShowHomeEnabled(true);
		actionBar.setTitle("Préférences");

		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		if (savedInstanceState != null) {
			Fragment fragmentOld = fragmentManager
					.findFragmentById(R.id.container_little);
			if (fragmentOld != null)
				fragmentTransaction.remove(fragmentOld);
			Fragment fragmentOld2 = fragmentManager
					.findFragmentById(R.id.container);
			if (fragmentOld2 != null)
				fragmentTransaction.remove(fragmentOld2);
		}
		FragmentConfiguration fragment = FragmentConfiguration
				.newInstance(liste);
		fragmentTransaction.add(R.id.container, fragment, "liste");
		fragmentTransaction.commit();
		fragmentManager.popBackStackImmediate();
	}

	private void loadCatOrder(String title){
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		FragmentConfigurationCatOrder fragment = new FragmentConfigurationCatOrder();
		if(this.isDualMode()){
			fragmentTransaction.add(R.id.container_little,fragment);
		}else{
			fragmentTransaction.addToBackStack("cat");
			fragmentTransaction.add(R.id.container,fragment);
		}
		fragmentTransaction.commit();
	}
	
	private void loadCatSelected(String title){
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		FragmentConfigurationCatSelected fragment = new FragmentConfigurationCatSelected();
		if(this.isDualMode()){
			fragmentTransaction.add(R.id.container_little,fragment);
		}else{
			fragmentTransaction.addToBackStack("cat");
			fragmentTransaction.add(R.id.container,fragment);
		}
		fragmentTransaction.commit();
	}

	@Override
	public void OnConfigSelected(int id) {
		//remove older fragment
		FragmentManager fragmentManager = getSupportFragmentManager();
		Fragment fragmentOld = fragmentManager
				.findFragmentById(R.id.container_little);
		if(fragmentOld != null){
			FragmentTransaction fragmentTransaction = fragmentManager
					.beginTransaction();
			fragmentTransaction.remove(fragmentOld);
			fragmentTransaction.commit();
		}
		//Select resources
		Resources res = getResources();
		String[] config = res.getStringArray(id);
		String title = config[1];
		Log.d("lemonde","View config "+title+",id="+id);
		switch (id) {
		case R.array.conf_cat_select:
			this.loadCatSelected(title);
			return;
		case R.array.conf_cat_order:
			this.loadCatOrder(title);
			return;
		case R.array.conf_zoom_level:
			this.zoomLevel(title);
			return;
		default: 
		//Si aucun, simplement boolean
		this.setPreferenceBoolean(title,config[0]);
		}
	}

	private void setPreferenceBoolean(String title, String key) {
		final CharSequence[] items = { "Oui", "Non" };
		final String finalKey = key;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		int posi = 0;
		if (!this.get_datas().getBooleanPreference(key))
			posi = 1;
		builder.setSingleChoiceItems(items, posi,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						boolean check = items[item].equals("Oui");
						ActivityConfiguration.this.get_datas().setBooleanPreference(finalKey, check);
						dialog.dismiss();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	private void zoomLevel(String title) {
		final CharSequence[] items = new CharSequence[8];
		int zoom = 80;
		int posi = 3;
		int current = this.get_datas().getZoomArticle();
		for(int i =0; i<items.length;i++){
			items[i]="Zoom "+zoom+" %";
			if(zoom==current)
				posi=i;
			zoom+=10;
		}
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setSingleChoiceItems(items, posi,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						int zoom = item*10+80;
						ActivityConfiguration.this.get_datas().setZoomArticle(zoom);
						dialog.dismiss();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	@Override
	public void onBackPressed() {
		if(this.isDualMode())
			this.finish();
		else
			super.onBackPressed();
	}

}
